package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.Utils.BaseContext;
import com.itheima.entity.ShoppingCart;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车业务处理层
 */
@Transactional
@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     */
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //获取当前用户id
        Long UserId = BaseContext.getCurrentId();
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //获取套餐id
        Long setmealId = shoppingCart.getSetmealId();
        //判断dishid是否为空 如果不为空 就是菜品数据
        //根据dishId 和 userId 作为条件查询购物车表 记录是否存在
        ShoppingCart sc = new ShoppingCart();
        if (dishId != null) {
            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getDishId, dishId);//菜品id
            lqw.eq(ShoppingCart::getUserId, UserId);//用户id
            sc = shoppingCartMapper.selectOne(lqw);
        } else {
            //2判断dishId是否为空 如果为空
            //根据setmealId 和 userId 作为条件查询购物车表 记录是否存在
            LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ShoppingCart::getSetmealId, setmealId);//菜品id
            lqw.eq(ShoppingCart::getUserId, UserId);//用户id
            sc = shoppingCartMapper.selectOne(lqw);
        }
        //3购物车记录不存在菜品或套餐数据 添加数据到购物车表中
        if (sc == null) {
            shoppingCart.setUserId(UserId);//某个用户的购物车
            shoppingCart.setNumber(1);//第一次添加菜品或套餐 设置数量为1
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
            return shoppingCart;
        }
        //购物车记录存在菜品或套餐数据 就修改购物车表中数量处理 number
        sc.setNumber(sc.getNumber() + 1);
        shoppingCartMapper.updateById(sc);
        return sc;
    }

    /**
     * 查询购物车
     */
    @Override
    public List<ShoppingCart> list() {
        //设置条件

//按加入购物车时间 将最新添加数据放最前面
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId()); //获取当前用户的购物车数据
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        return shoppingCartMapper.selectList(lqw);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //创建条件查询
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartMapper.delete(lqw);
        //执行删除
    }

    /**
     * 修改购物车
     *
     * @param shoppingCart
     */
    @Override
    public ShoppingCart update(ShoppingCart shoppingCart) {
        //设置条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //获取用户ID
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //获取菜品和套餐的id
        lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId()).or()
                .eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //查询一条数据
        ShoppingCart sc = shoppingCartMapper.selectOne(lqw);
        //判断
        if (sc.getNumber() == 1) {
            sc.setNumber(0);
            shoppingCartMapper.deleteById(sc);
        } else {
            sc.setNumber(sc.getNumber() - 1);
            shoppingCartMapper.updateById(sc);
        }
        return sc;
    }
}
